package wafteam.database;

import java.util.ArrayList;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class Database extends SQLiteOpenHelper {
 
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "comic";
 
    // Contacts table name
    private static final String TABLE_CHAP = "chaps";
 
    // Contacts Table Columns names
    private static final String CHAP_ID = "chapId";
    private static final String CHAP_NAME = "chap_name";
    private static final String NUMBER_IMAGE = "number_image";
    private static final String IS_READ = "is_read";
    private static final String IS_DOWNLOAD = "is_download";
    private static final String SIZE = "size";
 
    public Database(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CHAPS_TABLE = "CREATE TABLE " + TABLE_CHAP + "("
                + CHAP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + CHAP_NAME +" TEXT,"+ NUMBER_IMAGE + " INTEGER,"
                + IS_READ + " BOOLEAN," + IS_DOWNLOAD + " INTERGER," + SIZE + " INTERGER" + ")";
        db.execSQL(CREATE_CHAPS_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHAP);
 
        // Create tables again
        onCreate(db);
    }
 
    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */
 
    // Adding new contact
    public void addChap(Chap chap) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(CHAP_NAME, chap.getChapName());
        values.put(NUMBER_IMAGE, chap.getNumberImage());
        values.put(IS_READ, chap.getIsReaded()); 
        values.put(IS_DOWNLOAD, chap.getIsDownload());
        values.put(SIZE, chap.getSize());

        // Inserting Row
        db.insert(TABLE_CHAP, null, values);
        db.close(); // Closing database connection
    }
 
    // Find a chap by ID
    public Chap findChapById(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(TABLE_CHAP, new String[] { CHAP_NAME,
        		NUMBER_IMAGE, IS_READ, IS_DOWNLOAD, SIZE }, CHAP_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Chap contact = new Chap(cursor.getString(0), Integer.parseInt(cursor.getString(1)),
        		Integer.parseInt(cursor.getString(2)), Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)));

        return contact;
    }
     
    // Getting All Contacts
    public ArrayList<Chap> getAllChaps() {
        ArrayList<Chap> chapList = new ArrayList<Chap>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CHAP;
 
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Chap chap = new Chap();
                chap.setChapId(Integer.parseInt(cursor.getString(0)));
                chap.setChapName(cursor.getString(1));
                chap.setNumberImage(Integer.parseInt(cursor.getString(2)));
                chap.setIsReaded(Integer.parseInt(cursor.getString(3)));
                chap.setIsDownload(Integer.parseInt(cursor.getString(4)));
                chap.setSize(Integer.parseInt(cursor.getString(5)));
                chapList.add(chap);
            } while (cursor.moveToNext());
        }
        
        db.close();
        return chapList;
    }
 
    // Updating single chapter
    public int updateChap(Chap chap) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(CHAP_ID, chap.getChapId());
        values.put(CHAP_NAME, chap.getChapName());
        values.put(NUMBER_IMAGE, chap.getNumberImage());
        values.put(IS_READ, chap.getIsReaded());
        values.put(IS_DOWNLOAD, chap.getIsDownload());
        values.put(SIZE, chap.getSize());
 
        // updating row
        return db.update(TABLE_CHAP, values, CHAP_ID + " = ?",
                new String[] { String.valueOf(chap.getChapId()) });
    }
 
    // Deleting single contact
    public void deleteChap(Chap chap) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CHAP, CHAP_ID + " = ?",
                new String[] { String.valueOf(chap.getChapId()) });
        db.close();
    }
 
 
    // Getting contacts Count
    public int getChapsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_CHAP;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
 
        // return count
        return cursor.getCount();
    }
    
    public void destroyAll(){
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("DROP TABLE IF EXISTS "+TABLE_CHAP);
		onCreate(db);
	}
 
}
